﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Linq;
using Commerce.Common;

namespace Commerce.Promotions
{
    public class BuyProductFromCategoryGetProductFree : Coupon
    {
        public BuyProductFromCategoryGetProductFree()
            : base()
        {

        }

        public BuyProductFromCategoryGetProductFree(string couponCode, CouponType couponType)
            : base(couponCode, couponType)
        {
        }

        private List<int> _categoryForCoupon;

        [SelectorType("CategoryPicker", multiSelect = true)]
        public List<int> CategoryForCoupon
        {
            get { return _categoryForCoupon; }
            set { _categoryForCoupon = value; }
        }

        private int _productToGetForFree;

        [SelectorType("ProductPicker")]
        public int ProductToGetForFree
        {
            get { return _productToGetForFree; }
            set { _productToGetForFree = value; }
        }

        decimal CalculateDiscountAmount(decimal itemAmount)
        {
            decimal result = 0;

            return result;
        }

        public override bool ApplyCouponToOrder(Commerce.Common.Order order)
        {
            bool doRedirect = false;

            //Validate the order to make sure it's good.  
            CouponValidationResponse validationResponse =
                ValidateCouponForOrder(order);
            if (!validationResponse.IsValid)
            {
                throw new ArgumentException("Coupon is not valid for order: " + validationResponse, "order");
            }

            List<int> selected = new List<int>();
            XDocument doc = XDocument.Parse(base.GetXmlData());

            if (doc.Descendants("CategoryForCoupon").Descendants("int").Count() > 0)
            {
                foreach (XElement element in doc.Descendants("CategoryForCoupon").Descendants("int"))
                {
                    selected.Add(Int32.Parse(element.Value));
                }
            }
            else
            {
                throw new ArgumentException("No categories selected for coupon.");
            }

            if (!order.CouponCodes.Contains(CouponCode))
            {
                order.DiscountAmount = 0;

                foreach (var o in order.Items)
                {
                    ProductCategoryMapCollection p = new SubSonic.Select(ProductCategoryMap.Columns.CategoryID)
                        .From(ProductCategoryMap.Schema)
                        .WhereExpression(ProductCategoryMap.Columns.CategoryID).In(selected)
                        .And(ProductCategoryMap.Columns.ProductID).IsEqualTo(o.ProductID)
                        .ExecuteAsCollection<ProductCategoryMapCollection>();

                    if (p.Count() > 0)
                    {
                        XDocument prodDoc = XDocument.Parse(base.GetXmlData());
                        int prodID = 0;

                        if (doc.Descendants("CategoryForCoupon").Descendants("int").Count() > 0)
                        {
                            foreach (XElement element in doc.Descendants("ProductToGetForFree"))
                            {
                                prodID = Int32.Parse(element.Value);
                            }
                        }
                        else
                        {
                            throw new ArgumentException("No categories selected for coupon.");
                        }

                        Product prod = new Product(prodID);
                        prod.Quantity = 1;
                        prod.OurPrice = 0;
                        
                        OrderController.AddItemToOrder(o.OrderID, prod);
                        
                        doRedirect = true;
                        
                        break;
                    }
                }

                //add a comment to the order
                order.CouponCodes = CouponCode;

                //save the order
                order.Save("Coupon System");

            }

            return doRedirect;
        }
    }
}
